package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.exception.ValidateException;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.shopping.constants.ShoppingRetCode;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.form.CartRequest;
import com.cskaoyan.shopping.form.CheckAllItemRequest;
import com.cskaoyan.shopping.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

/**
 * @Author: ZDongMing
 * @Date: 2022/8/25 11:21
 */
@RestController
@RequestMapping("/shopping")
public class CartController {

    @Autowired
    ICartService cartService;


    @DeleteMapping("carts/{uid}/{pid}")
    public ResponseData deleteCartItem(@PathVariable("uid") Long uid, @PathVariable("pid") Long pid) {
        if (uid == null || pid == null) {
            throw new ValidateException(ShoppingRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode(),
                    ShoppingRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }
        cartService.deleteCartItem(uid, pid);
        return new ResponseUtil().setData(null);
    }


    @DeleteMapping("items/{uid}")
    public ResponseData deleteCheckedItem(@PathVariable("uid") Long uid) {
        if (uid == null) {
            throw new ValidateException(ShoppingRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode(),
                    ShoppingRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }
        cartService.deleteCheckedItem(uid);
        return new ResponseUtil().setData(null);
    }


    @PutMapping("items")
    public ResponseData checkAllCartItem(@RequestBody CheckAllItemRequest checkAllItemRequest) {

        cartService.checkAllCartItem(checkAllItemRequest);
        return new ResponseUtil<>().setData(null, SysRetCodeConstants.SUCCESS.getMessage());
    }



    @GetMapping("carts")
    public ResponseData getCartList(HttpServletRequest request){
        String userInfo = request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        String userId = String.valueOf(uid);
        Long id=null;
        try {
            id=Long.parseLong(userId);
        }catch (Exception e){
            return null;
        }
        Collection<CartProductDto> cartList = cartService.getCartListById(id);
        return new ResponseUtil<>().setData(cartList,null);
    }


    @PostMapping("carts")
    public ResponseData addToCart(@RequestBody CartRequest cartRequest){
        cartService.addToCart(cartRequest);
        return new ResponseUtil<>().setData(null);
    }


    @PutMapping("carts")
    public ResponseData updateCart(@RequestBody CartRequest cartRequest){
        cartService.updateCartNum(cartRequest);
        return new ResponseUtil<>().setData(null);
    }
}
